package com.minio.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.minio.service.MinIOService;
import com.minio.util.MinioUtil;
import com.util.DateUtils;
import com.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

@Service("minIOService")
public class MinIOServiceImpl implements MinIOService {

	private static final Logger LOG = LoggerFactory.getLogger(MinIOServiceImpl.class);

    @Autowired
    MinioUtil minioUtil;

    @Override
    public Map<String, String> upFile(Map<String, List<MultipartFile>> fileMap, String bucket, String path) throws Exception{
        if(CollectionUtils.isEmpty(fileMap)){
            return null;
        }
        if(StringUtils.isEmpty(path)){
            path = DateUtils.format(new Date(),DateUtils.YYYYMMDD);
        }
        Map<String, String> map = new HashMap<>();
        for (Map.Entry<String, List<MultipartFile>> entry : fileMap.entrySet()) {
            List<MultipartFile> files = entry.getValue();
            if(StringUtil.isEmpty(entry.getKey()) || CollectionUtils.isEmpty(files) ){
               continue;
            }
            if(StringUtils.isEmpty(bucket)){
                bucket="anonymous";
            }
            minioUtil.createBucket(bucket);

            List<String> urls = new ArrayList<>();
            for (MultipartFile file : files) {

                String url = minioUtil.uploadFile(file.getInputStream(), bucket, path+"/"+file.getOriginalFilename());
                urls.add(url);
            }
            String uri = org.apache.commons.lang3.StringUtils.join(urls, "&&");
            map.put(entry.getKey(),uri);
        }
        return map;
    }

    @Override
    public Map<String, String> netFile(Map<String, List<String>> netFileMap, String bucket, String path) throws Exception {

        if(CollectionUtils.isEmpty(netFileMap)){
            return null;
        }
        if(StringUtils.isEmpty(path)){
            path = DateUtils.format(new Date(),DateUtils.YYYYMMDD);
        }
        Map<String, String> map = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : netFileMap.entrySet()) {
            List<String> netFiles = entry.getValue();
            if(StringUtil.isEmpty(entry.getKey()) || CollectionUtils.isEmpty(netFiles) ){
                continue;
            }
            if(StringUtils.isEmpty(bucket)){
                bucket="anonymous";
            }
            minioUtil.createBucket(bucket);

            List<String> urls = new ArrayList<>();
            for (String url : netFiles) {
                URL urlstream = new URL(url);
                HttpURLConnection httpcon = (HttpURLConnection) urlstream.openConnection();
                httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
                //获取文件名
                String fileName = minioUtil.getUrlName(url);
                String uri = minioUtil.uploadFile(httpcon.getInputStream(), bucket, path+"/"+ fileName);
                urls.add(uri);
            }
            String uri = org.apache.commons.lang3.StringUtils.join(urls, "&&");
            map.put(entry.getKey(),uri);
        }
        return map;
    }

    @Override
    public Map<String, String> netFile(Map<String, List<String>> netFileMap, String bucket, String path, String origin) throws Exception {
        if(CollectionUtils.isEmpty(netFileMap)){
            return null;
        }
        if(StringUtils.isEmpty(path)){
            path = DateUtils.format(new Date(),DateUtils.YYYYMMDD);
        }
        Map<String, String> map = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : netFileMap.entrySet()) {
            List<String> netFiles = entry.getValue();
            if(StringUtil.isEmpty(entry.getKey()) || CollectionUtils.isEmpty(netFiles) ){
                continue;
            }
            if(StringUtils.isEmpty(bucket)){
                bucket="anonymous";
            }
            minioUtil.createBucket(bucket);

            List<String> urls = new ArrayList<>();
            for (String url : netFiles) {
                //http://sshare666.com/的图片实际会跳转到https上去，所以要替换
                if(StringUtils.hasText(origin)){
                if(Objects.equals(origin.trim(),"来自2048")){
                    url = url.replace("http:","https:");
                }}
                URL urlstream = new URL(url);
                HttpURLConnection httpcon = (HttpURLConnection) urlstream.openConnection();
                httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
                //获取文件名
                String fileName = minioUtil.getUrlName(url);
                String uri = minioUtil.uploadFile(httpcon.getInputStream(), bucket, path+"/"+ fileName);
                urls.add(uri);
            }
            String uri = org.apache.commons.lang3.StringUtils.join(urls, "&&");
            map.put(entry.getKey(),uri);
        }
        return map;
    }
}
